<template>
  <el-dialog
    title="黑子权限分配"
    :visible="showRoleDialog"
    width="40%"
    :before-close="handleClose"
  >
    <el-checkbox-group v-model="rolesIds">
      <el-checkbox v-for="item in rolesList" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="hEdit">确 定</el-button>
      <el-button @click="handleClose">取 消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getRolesEnabled } from '@/api/role'
import { getEmployeeDetail, assignRoles } from '@/api/employee'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      required: true
    },
    codId: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      rolesIds: [],
      rolesList: []
    }
  },
  created() {
    this.getRoles()
  },
  methods: {
    // 分配员工角色
    async hEdit() {
      await assignRoles({ id: this.codId, roleIds: this.rolesIds })
      this.$message.success('角色分配成功')
      this.handleClose()
    },
    // 回显员工已有角色
    async getRolesUser() {
      const res = await getEmployeeDetail(this.codId)
      console.log(res)
      this.rolesIds = res.data.roleIds
    },
    // 获取角色
    async getRoles() {
      const res = await getRolesEnabled()
      this.rolesList = res.data
    },
    // 关闭
    handleClose() {
      this.$emit('update:showRoleDialog', false)
    }
  }
}
</script>

<style>

</style>
